package com.txh.blog.controller.type;

import com.txh.blog.exceprtion.exceptions.NullAttrException;
import com.txh.blog.model.Type;
import com.txh.blog.service.TypeService;
import com.txh.blog.util.ResultJsonUtils;
import com.txh.blog.vo.ResultJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

@Controller
@RequestMapping("/type")
public class TypeController {

    @Autowired
    private TypeService typeService;
    @RequestMapping("list")
    public String typeList(Integer pageNum, Integer pageSize, Model model){
        Map<String,Object> page = typeService.getTypeListPage(pageNum,pageSize);
        model.addAttribute("page",page);
        return "admin/blogTypeList";
    }

    @RequestMapping("/del")
    @ResponseBody
    public ResultJson delteType(Long id){
        if (id == null){
            throw  new NullAttrException();
        }
        typeService.deleteTypeById(id);
        return ResultJsonUtils.success(null);
    }

    @RequestMapping("/add")
    @ResponseBody
    public ResultJson add(Type type){
        if (type == null){
            throw new NullAttrException();
        }
        typeService.addType(type);
        return ResultJsonUtils.success(type);
    }
}
